package com.abe.config;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.abe.app.ManagerAPP;

import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Environment;

public class Constants {

	/** 大屏命令组装 */
	public static String getSocketCommand(String name, Object cll,
			Object speed, Object time, Object distance, Object slope,
			Object heart, Object headimg) {
		/** 同步大屏数据 */
		/** run<<>>用户名<<>>累积卡路里<<>>速度<<>>时间<<>>距离<<>>坡度<<>>心律<<>>头像url */
		StringBuilder builder = new StringBuilder();
		/** 分隔符 */
		String separator = "<<>>";
		/** 数据拼接 */
		builder.append("run").append(separator).append(name).append(separator)
				.append(cll).append(separator).append(speed).append(separator)
				.append(time).append(separator).append(distance)
				.append(separator).append(slope).append(separator)
				.append(heart).append(separator).append(headimg);
		return builder.toString();
	}

	/** 获取Android下所有应用 */
	public static List<ResolveInfo> loadApps() {
		List<ResolveInfo> showApps, needApps;
		needApps = new ArrayList<ResolveInfo>();
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		showApps = ManagerAPP.getContext().getPackageManager()
				.queryIntentActivities(mainIntent, 0);
		/** 去掉自己 */
		String selfPackageName = ManagerAPP.getContext().getPackageName();
		for (int i = 0; i < showApps.size(); i++) {
			ResolveInfo resolveInfo = showApps.get(i);
			String tempPackage = resolveInfo.activityInfo.packageName;
			if (!selfPackageName.equals(tempPackage)) {
				needApps.add(resolveInfo);
			}
		}
		return needApps;
	}

	/** 意图 */
	public static final int SettingLanguage = 0;
	public static final int ExitApp = 1;

	/**
	 * 服务器地址
	 */
	public static String BASE_URL = "http://tz.jileniao.net/api/";
	public static String BASE_NET_URL = "http://tz.jileniao.net/";

	/** 初始化APP相关文件夹 */
	public static void initAPPFolder() {
		/** 入口 */
		getRootPath();
		/** Image缓存 */
		getImageDirPath();
		/** Video相关 */
		File VideoBaseFile = new File(VideoBase);
		if (!VideoBaseFile.exists()) {
			VideoBaseFile.mkdirs();
		}
		/** Video预览图片相关 */
		File VideoImagesBaseFile = new File(VideoImagesBase);
		if (!VideoImagesBaseFile.exists()) {
			VideoImagesBaseFile.mkdirs();
		}
	}

	/** APP文件夹 */
	public static String FileBase = "StrengthTrainer/AbeFiles";

	/** 图片保存文件夹 */
	public static String ImageBase = "AbeImage";

	/** 视频更新视频文件夹地址 */
	public static String VideoBase = getRootPath() + "/video";

	/** 视频更新预览图文件夹地址 */
	public static String VideoImagesBase = getRootPath() + "/videoImage";

	/** 软件更新下载位置 */
	public static File getUpdateAPP() {
		File file = new File(getRootPath() + "/updateFile/newestApp.apk");
		if (!file.exists()) {
			file.mkdirs();
		}
		return file;
	}

	/** 菜单截图 */
	public static File getMainMenuShot() {
		File file = new File(getImageDirPath() + "/menushot.jpg");
		if (!file.exists()) {
			file.mkdirs();
		}
		return file;
	}

	/** 获取APP缓存文件根目录 */
	public static String getRootPath() {
		String path = Environment.getExternalStorageDirectory().getPath();
		if (path != null) {
			File dir = new File(path + "/" + FileBase);
			if (!dir.exists()) {
				dir.mkdirs();
			}
			return dir.getAbsolutePath();
		} else {
			return path;
		}
	}

	/** 获取图片缓存路径 */
	public static String getImageDirPath() {
		File iamgeDir = new File(getRootPath() + "/" + ImageBase);
		if (!iamgeDir.exists()) {
			iamgeDir.mkdirs();
		}
		return iamgeDir.getAbsolutePath();
	}

	/** 公共顶部信息标识 */
	public static final int MainTopTime = 0;
	public static final int MainTopHeartRate = 1;
	public static final int MainTopSteps = 2;
	public static final int MainTopCalorie = 3;
	public static final int MainTopDistance = 4;

	/** 0：初始状态,1：视频画廊展示，2：视频播放不可见，3：视频播放可见 */
	public static final int VideoGone = 0;
	public static final int CoverFlowVisible = 1;
	public static final int VideoPlayGone = 2;
	public static final int VideoPlayVisible = 3;

	/** 默认速度 km/h */
	public static final float SpeedStart = 1;
	/** 默认坡度 */
	public static final float SlopeStart = 0;

	/** 最大速度 */
	public static final int SpeedMax = 20;
	/** 最大坡度 */
	public static final int SlopeMax = 15;

	/** m/s转km/h */
	public static float SpeedConvertByMS(float msSpeed) {
		return (float) (msSpeed * 3.6);
	}

	/** km/h转m/s */
	public static float SpeedConvertByKMH(float kmhSpeed) {
		return (float) (kmhSpeed / 3.6);
	}

	/**
	 * 倒计时格式化：（hh:mm：ss） "0:00:00"; "00:00";
	 * 
	 * @param time
	 *            单位s
	 * */
	public static String formatTime(int time) {
		String formatTime = "";
		int h = time / 3600;// 小时
		if (h > 0) {
			formatTime = h + ":";
		}
		int m = (time % 3600) / 60;// 分钟
		if (m > 9) {
			formatTime = formatTime + m + ":";
		} else {
			formatTime = formatTime + "0" + m + ":";
		}
		int s = time % 60;// 秒
		if (s > 9) {
			formatTime = formatTime + s;
		} else {
			formatTime = formatTime + "0" + s;
		}
		return formatTime;
	}

	/**
	 * 每公里330千卡，卡路里单位1kc
	 * 
	 * @param distance
	 *            米
	 * */
	public static float getKcal(float distance) {
		return distance * 330 / (float) 1000;
	}

	/** 保留x位小数 */
	public static float keepDecimal(float a, int x) {
		int y = 1;
		for (int i = 1; i <= x; i++) {
			y = y * 10;
		}
		return (float) (Math.round(a * y)) / y;
	}

	public static float StepLength = 0.73f;

	/** 步长为0.73米 步数=距离除以步长 */
	public static int getSteps(float distance) {
		return (int) (distance / StepLength);
	}

	/********************** 力量器材相关 *************************/
	/**
	 * @see 1公斤·米(kg·m)=0.0023422千卡(kcal)
	 * @param weight
	 *            重量（KG）
	 * @param height
	 *            高度（m）
	 * @param count
	 *            次数
	 * 
	 * */
	public static float getKcal(float weight, float height, int count) {
		return (float) (weight * height * count);
	}
}
